@extends('Admin.layout.layout')
@section('content')
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>修改</h5>
                    </div>
                    <div class="ibox-content">
                        <form class="form-horizontal m-t" id="signupForm">

                            {{csrf_field()}}
                            <input  name="id" value="{{$data['id']}}" class="form-control" type="hidden">
                            <div class="form-group">
                                <label class="col-sm-3 control-label">标题：</label>
                                <div class="col-sm-5">
                                    <input id="" name="title" value="{{$data['title']}}" class="form-control" type="text">
                                </div>
                            </div>
                            <div class="form-group">

                                <label class="col-sm-3 control-label">广告位置：</label>
                                <div class="col-sm-8">
                                    <select name="position" class="form-control">
                                        <option value="0">请选择</option>
                                        @foreach($advertisement_position as $k=>$v)
                                            <option @if($data['position']==$v['id']) selected @endif value="{{$v['id']}}">{{$v['title']}}</option>
                                        @endforeach
                                    </select>
                                    @include('Admin.layout.tip',['field' => 'position'])
                                </div>
                            </div>

                            <div class="form-group">

                                <label class="col-sm-3 control-label">楼层选择：(需要时选择)</label>
                                <div class="col-sm-8">
                                    <select name="typeid" class="form-control">
                                        <option value="0">请选择</option>
                                        @foreach($category as $k=>$v)
                                            @if($v['pid'] == 0)
                                                <option @if($data['typeid']==$v['id']) selected @endif value="{{$v['id']}}">{{$v['name']}}</option>
                                            @endif
                                        @endforeach
                                    </select>
                                    @include('Admin.layout.tip',['field' => 'typeid'])
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-3 control-label">图片上传：</label>
                                <div class="col-sm-1">
                                    <img id="imgContent" style="width: 80px;height: 80px;" src="{{$data['image']}}">
                                    <div class="btn-group">
                                        <label title="上传图片" for="inputImage" class="btn btn-primary">
                                            <input type="file" accept="image/*" name="file" id="inputImage" class="hide" multiple="false"> 上传图片
                                            <input type="hidden" value="{{$data['image']}}" id="add_logo" name="image" />
                                        </label>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-3 control-label">文字广告：</label>
                                <div class="col-sm-5">
                                    <input id="" name="textads" value="{{$data['textads']}}" class="form-control" type="text" placeholder='如果是多个文字广告请用"-"隔开'>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-3 control-label">链接路径：</label>
                                <div class="col-sm-5">
                                    <input id="" name="url" value="{{$data['url']}}" class="form-control" type="text">
                                </div>
                            </div>


                            <div class="form-group">
                                <label class="col-sm-3 control-label">审核：</label>
                                <div class="col-sm-5">
                                    <div class="radio mrd10">
                                        <input type="radio" name="audit" id="radio4" value="1"  {{$data['audit']==1?"checked='checked'":""}} class="" style="margin:2px 0 0 0;">
                                        <label for="radio4"> 是</label>
                                    </div>
                                    <div class="radio mrd10">
                                        <input type="radio" name="audit" id="radio3" value="0" {{$data['audit']==0?"checked='checked'":""}} class="" style="margin:2px 0 0 0;">
                                        <label for="radio3"> 否</label>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-3 control-label">排序：</label>
                                <div class="col-sm-1">
                                    <input id="" name="sort" value="{{$data['sort']}}" class="form-control" type="text">
                                </div>
                            </div>


                            <div class="modal-footer">
                                <a href="/foradmin/advertisinglist" class="btn btn-default pull-left">返回</a>
                                <button type="button" class="btn btn-primary" id="btn_submit">
                                    确定
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

    </div>
    <script src="{!!asset('/laravel-u-editor/ueditor.config.js')!!}"></script>
    <script src="{!!asset('/laravel-u-editor/ueditor.all.min.js')!!}"></script>
    <script>
        //新增上传图片
        function upload(file,id){
            var reader = new FileReader();
            //创建文件读取相关的变量
            var imgFile;
            //为文件读取成功设置事件
            reader.onload=function(e) {
                imgFile = e.target.result;
                $("#"+id).attr('src', imgFile);
            };
            //正式读取文件
            reader.readAsDataURL(file);
        }
        $('#inputImage').change(function(img){
            upload(this.files[0],"imgContent");
            var files =  this.files[0];
            var formFile = new FormData();
            formFile.append("image", files);
            $.ajax({
                url: "/uploads/images",
                type: 'post',
                data: formFile,
                dataType:"json",
                contentType: false,
                processData: false,
                success: function(data) {
                    $("#add_logo").val(data.path);
                },
                error: function(data) {
                    console.log("error");
                }
            });
        })


        $('#btn_submit').click(function(){
            $.ajax({
                url: '{{url('/foradmin/advertisinglist/'.$data['id'])}}',
                type: 'put',
                dataType: 'json',
                data: $('#signupForm').serialize(),
                success: function (data) {
                    if (0 == data.code) {
                        layer.alert(data.message?data.message:data.data, {
                            icon: 1
                        }, function () {
                            url = '/foradmin/advertisinglist';
                            location.href = url;
                        });
                    } else {
                        layer.alert(data.message, {
                            icon: 2
                        });
                    }
                }
            });
        });
    </script>
@endsection
